package com.cdacp.openapi.example;

import com.alibaba.fastjson.JSON;
import com.cdacp.openapi.common.Signature;
import com.cdacp.openapi.test.TestBase;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 代收款接口
 */
public class CollectionTest extends TestBase {

    String url = "http://localhost:8081";
    String appId = "20210112798540824588582912";
    String privateKey ="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDOFxFEBDlz/VxouCQgO94wvtfNiE1KvteG73oVth+8F9RSBmV5mU1pcj7j61wJpdegV544iufDZJZzqrABwt1s38FXFeKt/z3HzW0KPNARDabNSq0b/24MGWF+IoA++02p/xTStWYLnS1y2x+Hi6R5VG+fFLHyufefo2RVr8Ij3GPAfYerooWTBYJ6rDeLY5PFkDOCk3AaxdRlq9AD+FCc65Djifl2sVa2EfSgJIZVvBJNiBLWo4DyRZJuxeB+zaqNoS9r9mSBWwyu5lufaRp2lubL3hj72HsRwA6GOkiixW9RX1fDhF3z/KeqX5GZcKQdC17Y5KXJDzdXbH9yNf5fAgMBAAECggEBAJF28rbhjHP0Vy+LGeSunSwm6Eeod3XUsmgJzFBa1pd68mBbJ3Dm5h8RhpZ1X+dnaYVBkuoyEVh3PUKzLbxwgL4CSYNgbTmHOWI3+0kIl4wR6LRqizmB13kuyQOw8niCBNWCuH3pj/eOn6+8Psr7o8IH+Sl+AARlgwTo3T8l6ZBJEA2APgx2j2GNXH280a5wYijS5ej/9m1IfyZUeNvZ0VsX9pMd2hLM+7SpQJOpPPbVvkzzLubmBldMrBmAj+id9CQDHBxI7kcuSRxGSune6Jiq75G710NLDI9L9um9j/zZoiFrLf7e89W49y4WjShnR8s6ORsH1sCZadVQuAnOGlECgYEA9j9mDYhEp9TMPbTwQ+lmWP/EDwKMbqEk2jCD/Rs+O52KCn8t9Q4ZKYtxqEzBLH4O+tgQK4cCT8EkBiCaxGr2oLgtczmMrYVPAVybPEgSg87OjoQ8CcHFRhgpSVOG02MplcBTWKCrc47qsaqixNzAjBEnS+fSQUZIfIq8OzWAucMCgYEA1kCHQkiH5MKAx/LO7+gmsDZYfw/cgaad17bNIl31i62zRECsNkG/7mCwlXuF4XlFLPX5jyEG2R/vsD5K7zjUyY9fFLbDOLGqIcarYJlq1HakRZISLSkVWAx1JLsMqM12gsjseikeKpk98QursTdBpEeaxwn+GC5YkF7arEmwwzUCgYEA0zqU9vyyL+ko/bGs/ARFCAj1jA6amfde3k8JmV76JjdABzsLGA9s5Cc6aqzUc676eJBIZp4cDH/Xv+3N9LjwL/NtAU8Xbm1Q1RE0FCSaxTfsFXI28gOmjjcBi2W73K5OKKfelsLT739jsN13XDkkbcH1HHaR8+4vFa4IQx3k+vMCgYANBxVyaViiB1WvUstUeShJFLTi3QMKeygj4sCsc0wn9yLsZoZSMkNKamSyYjVs1WPobPiExHxXHYMEk3UZALqnmuksKETMoLj4nszy7cE6NBCfDle+mT3sAa3p0tndNckcAL2oW/Gyo2jJzGzfNL3wEE6QBcKGk+LcvHfNfBmcdQKBgDLkmZdbXiUbGw4Rx640i47LHdkv4kuyYv7lCJq5vbWeIw4zUsG4PBg7Q4JkWqa8902FRZuGsG6HwEHoaB2RJGVherv6ovh7++sDP6/3J8iRWl6ZuQFErhCzidWk2VdnltmrVATEAY31l1mNHJ2vFBT9NFgYlc91JE49rp7PQY2L";


    /**
     参数	            类型	    是否必填	    最大长度	    描述	            示例值
     app_id	        String	是	        32	    cdacp分配给开发者的应用ID	2014072300007148
     method	        String	是	        128	    接口名称	trade.fastpay.refund.query
     format	        String	否	        40	    仅支持JSON	JSON
     charset	        String	是	        10	    请求使用的编码格式，如utf-8,gbk,gb2312等	utf-8
     sign_type	    String	是	        10	    商户生成签名字符串所使用的签名算法类型，目前支持RSA2和RSA，推荐使用RSA2	RSA2
     sign	        String	是	        344	    商户请求参数的签名串，详见签名	详见示例
     timestamp	    String	是	        19	    发送请求的时间，格式"yyyy-MM-dd HH:mm:ss"	2014-07-24 03:07:50
     version	        String	是	        3	    调用的接口版本，固定为：1.0	1.0
     app_auth_token	String	否	        40	    详见应用授权概述
     biz_content	    String	是		请求参数的集合，最大长度不限，除公共参数外所有请求参数都必须放在这个参数中传递，具体参照各产品快速接入文档
     */
    // 这个请求会路由到服务


    //预订单下单
    @Test
    public void testGet() throws Exception {

        // 公共请求参数
        Map<String, String> params = new HashMap<String, String>();
        params.put("app_id", appId);
        params.put("method", "collection.api.param.post");//方法名
        params.put("format", "json");
        params.put("charset", "utf-8");
        params.put("sign_type", "RSA2");
        params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        params.put("version", "1.0");

        // 业务参数
        Map<String, Object> bizContent = new HashMap<>();
        bizContent.put("packageName", "com");
        bizContent.put("appId", appId);
        bizContent.put("preOrderNo","42a938708c564133be404216c8f50054");
        bizContent.put("code","182185");
        params.put("biz_content", JSON.toJSONString(bizContent));
        String content = Signature.getSignContent(params);
        String sign = Signature.rsa256Sign(content, privateKey, "utf-8");
        params.put("sign", sign);

        System.out.println("----------- 请求信息 -----------");
        System.out.println("请求参数：" + buildParamQuery(params));
        System.out.println("商户秘钥：" + privateKey);
        System.out.println("待签名内容：" + content);
        System.out.println("签名(sign)：" + sign);
        System.out.println("URL参数：" + buildUrlQuery(params));

        System.out.println("----------- 返回结果 -----------");
        String responseData = post(url, params);// 发送请求
        System.out.println(responseData);
    }




}
